using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Windows.Forms;
using DevExpress.Utils.Extensions;
using DevExpress.XtraGrid.Views.Grid;

namespace ProtoWinform
{
    public static class ExGridView
    {
        
        public static List<T> GetSelectedObjects<T>(this GridView gridView)
        {
            List<T> selectedRows = new List<T>();

            foreach (int rowIndex in gridView.GetSelectedRows())
            {
                T rowObject = (T)gridView.GetRow(rowIndex);
                selectedRows.Add(rowObject);
            }

            return selectedRows;
        }        
        
        public static T GetSelectedObject<T>(this GridView gridView)
        {
            return  (T)gridView.GetFocusedRow();
        }
        
        
              
        public static void FormatFontCondionaly (this GridView gridView, Func< RowCellStyleEventArgs,bool> predicate , Color alterFontColor=default)
        {
            gridView.RowCellStyle += (s, e) =>
            {
                if (predicate(e))
                {
                    e.Appearance.ForeColor = alterFontColor == default ? Color.Coral : alterFontColor;
                    // e.Appearance.FontStyleDelta = alterFontColor == default ? Color.Coral : alterFontColor;
                    e.Appearance.Font = new Font(e.Appearance.Font, FontStyle.Bold);
                }
            };
            
        }
        
        
        
                      
        public static void ExcelEx (this GridView gv , string typename="T" )
        {

            FilenameSelector filenameSelector = new FilenameSelector();
            string type =   typename ; // Replace with the actual type of data being exported
            string defaultFilename = filenameSelector.GenerateDefaultFilename(type);
            string selectedFilename = filenameSelector.PromptUserForFilename(defaultFilename);

            if (selectedFilename != null)
            {
                // Export data to the selected Excel file
                gv.ExportToXls( selectedFilename );
                filenameSelector.AskUserToOpenFile(selectedFilename);
            }



        }
        
         

        public class FilenameSelector
        {
            public void AskUserToOpenFile(string filename)
            {
                DialogResult result = MessageBox.Show("要打开吗", "打开", MessageBoxButtons.YesNo);
                if (result == DialogResult.Yes)
                {
                    // Open the file with the default application
                    try
                    {
                        Process.Start(filename);
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show($"windows 不能打开它，可能需要安装 Excel: {ex.Message}");
                    }
                }
            }
            public string GenerateDefaultFilename(string type)
            {
                // Generate a default filename based on the type and current datetime
                string datetime = DateTime.Now.ToString("yyyyMMddHHmmss");
                string defaultFilename = $"{type}_{datetime}.xls";
                return defaultFilename;
            }

            public string PromptUserForFilename(string defaultFilename)
            {
                // Display a SaveFileDialog to allow the user to modify the filename
                SaveFileDialog saveFileDialog = new SaveFileDialog();
                saveFileDialog.Filter = "Excel files (*.xls)|*.xls";
                saveFileDialog.FileName = defaultFilename;
                if (saveFileDialog.ShowDialog() == DialogResult.OK)
                {
                    return saveFileDialog.FileName;
                }
                return null;
            }
        }








}
}